package com.yiwei.zhh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yiwei.zhh.R;
import com.yiwei.zhh.api.Api;
import com.yiwei.zhh.api.bean.MiBaoVerifyBean;
import com.yiwei.zhh.api.bean.RetrieveByPhoneV3Bean;
import com.yiwei.zhh.event.FindXueXinSuccessEvent;
import com.yiwei.zhh.model.MiBaoQuestionModel;
import com.yiwei.zhh.util.Network;
import com.yiwei.zhh.util.RxLifeUtil;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.util.UI;
import com.yiwei.zhh.view.FlowViewHorizontal;
import com.yiwei.zhh.view.MiBaoQuestionDialog;
import com.yiwei.zhh.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3XueXinMiBaoQuestionActivity extends BaseActivity implements MiBaoQuestionDialog.OnSureListener {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_tips1)
    TextView txtTips1;
    @BindView(R.id.txt_tips2)
    TextView txtTips2;
    @BindView(R.id.txt_question_one)
    TextView txtQuestionOne;
    @BindView(R.id.btn_question_one)
    FrameLayout btnQuestionOne;
    @BindView(R.id.edit_answer_one)
    EditText editAnswerOne;
    @BindView(R.id.txt_question_two)
    TextView txtQuestionTwo;
    @BindView(R.id.btn_question_two)
    FrameLayout btnQuestionTwo;
    @BindView(R.id.edit_answer_two)
    EditText editAnswerTwo;
    @BindView(R.id.txt_question_three)
    TextView txtQuestionThree;
    @BindView(R.id.btn_question_three)
    FrameLayout btnQuestionThree;
    @BindView(R.id.edit_answer_three)
    EditText editAnswerThree;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.linearLayout)
    LinearLayout linearLayout;
    @BindView(R.id.step_view)
    FlowViewHorizontal stepView;
    private Unbinder unbinder;
    private MiBaoQuestionDialog miBaoQuestionDialog;
    private List<MiBaoQuestionModel.DataBean> dataBeanList;
    private int seletPoi = 0;
    private String keyOne;
    private String keyTwo;
    private String keyThree;
    private String ctoken;
    private String account;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xuexin_find_way_question_v3);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    @OnTextChanged({R.id.edit_answer_one, R.id.edit_answer_two, R.id.edit_answer_three})
    void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(UI.toString(editAnswerOne)) && !TextUtils.isEmpty(UI.toString(editAnswerTwo)) && !TextUtils.isEmpty(UI.toString(editAnswerThree))) {
            btnSubmit.setSelected(true);
        } else {
            btnSubmit.setSelected(false);
        }
    }

    private void init() {
        String[] stepTitles = new String[]{"选择方式", "验证账户", "重置密码"};
        stepView.setProgress(2, 3, stepTitles, null);
        account = getIntent().getStringExtra("account");
        ctoken = getIntent().getStringExtra("ctoken");
        miBaoQuestionDialog = new MiBaoQuestionDialog(this).setOnSelect(this);
        getXueXinQuestions();
    }

    public void getXueXinQuestions() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();

        Api.api_service.getXueXinQuestions().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(model -> {
                    setQuestion(model.data);
                });
    }

    public void getMiBaoVerify() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        MiBaoVerifyBean bean = new MiBaoVerifyBean();
        bean.ctoken = ctoken;
        bean.pwdreq1 = keyOne;
        bean.pwdreq2 = keyTwo;
        bean.pwdreq3 = keyThree;
        bean.pwdanswer1 = UI.toString(editAnswerOne);
        bean.pwdanswer2 = UI.toString(editAnswerTwo);
        bean.pwdanswer3 = UI.toString(editAnswerThree);
        Api.api_service.getMiBaoVerify(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(model -> {
                    Intent intent = new Intent(this, V3XueXinMiBaoResetActivity.class);
                    intent.putExtra("account", account);
                    intent.putExtra("verifycode", model.datas.verifycode);
                    startActivity(intent);
                    finish();
                    overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                });
    }

    private void setQuestion(List<MiBaoQuestionModel.DataBean> data) {
        dataBeanList = data;
        miBaoQuestionDialog.setData(data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_question_one, R.id.btn_question_two, R.id.btn_question_three, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_question_one:
                seletPoi = 0;
                if (dataBeanList == null) {
                    getXueXinQuestions();
                } else {
                    miBaoQuestionDialog.show();
                }
                break;
            case R.id.btn_question_two:
                seletPoi = 1;
                if (dataBeanList == null) {
                    getXueXinQuestions();
                } else {
                    miBaoQuestionDialog.show();
                }
                break;
            case R.id.btn_question_three:
                seletPoi = 2;
                if (dataBeanList == null) {
                    getXueXinQuestions();
                } else {
                    miBaoQuestionDialog.show();
                }
                break;
            case R.id.btn_submit:
                if (codeViewInputCheck()) {
                    getMiBaoVerify();
                }
                break;
        }
    }

    private boolean codeViewInputCheck() {
        if (TextUtils.isEmpty(UI.toString(editAnswerOne))) {
            ToastUtil.makeText(getApplicationContext(), "第一个答案不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(UI.toString(editAnswerTwo))) {
            ToastUtil.makeText(getApplicationContext(), "第二个答案不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(UI.toString(editAnswerThree))) {
            ToastUtil.makeText(getApplicationContext(), "第三个答案不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    @Override
    public void onSure(String id, String name) {
        switch (seletPoi) {
            case 0:
                keyOne = id;
                txtQuestionOne.setText(name);
                break;
            case 1:
                keyTwo = id;
                txtQuestionTwo.setText(name);
                break;
            case 2:
                keyThree = id;
                txtQuestionThree.setText(name);
                break;
        }
    }
}
